package com.js.nickcar.module.addcar.check;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.js.nickcar.R;
import com.js.nickcar.entity.local.BaseCarInfoEntity;
import com.js.nickcar.module.addcar.baseinfo.BaseCarInfoHolder;
import com.js.nickcar.module.addcar.baseinfo.BaseCarInfoNextHolder;
import com.js.nickcar.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by LZY on 2017/9/28.
 */
public class CheckCarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements BaseCarInfoEntity.ItemType {
    public final LayoutInflater mInflater;
    public final Context mContext;
    public List<BaseCarInfoEntity> mData = new ArrayList<>();
    public OnItemClickListener onItemClickListener;

    public CheckCarAdapter(Context context, List<BaseCarInfoEntity> data) {
        mContext = context;
        mInflater = LayoutInflater.from(context.getApplicationContext());
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_SELECT:
            case TYPE_INPUT:
                return new BaseCarInfoHolder(mInflater.inflate(R.layout.item_base_car_info, parent, false));
            default:
                return new BaseCarInfoNextHolder(mInflater.inflate(R.layout.item_base_car_info_next, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BaseCarInfoEntity item = getItem(position);
        switch (item.getType()) {
            case TYPE_SELECT:
            case TYPE_INPUT:
                onBindBaseCarInfoHolder((BaseCarInfoHolder) holder, position);
                break;
            case TYPE_NEXT:
                onBindNextHolder((BaseCarInfoNextHolder) holder, position);
                break;
        }
    }

    private void onBindBaseCarInfoHolder(BaseCarInfoHolder holder, final int position) {
        final BaseCarInfoEntity item = getItem(position);
        holder.tvTitle.setText(item.getTitle());
        holder.tvContent.setHint(StringUtils.getSizeSpan(item.getType() == TYPE_SELECT ?
                getString(R.string.please_select) : getString(R.string.please_input), 13));
        holder.tvContent.setText(item.getContent());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onNorItemClick(item, position);
                }
            }
        });
    }

    @NonNull
    private String getString(int resId) {
        return mContext.getResources().getString(resId);
    }

    private void onBindNextHolder(BaseCarInfoNextHolder holder, int position) {
        holder.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onNext(mData);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    public BaseCarInfoEntity getItem(int position) {
        return mData.get(position);
    }

    public List<BaseCarInfoEntity> getAllData() {
        return mData;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onNorItemClick(BaseCarInfoEntity entity, int position);

        void onNext(List<BaseCarInfoEntity> lists);
    }

}
